package com.citi.xylophone.phoneEmulator.core;

import java.util.List;

import org.apache.commons.httpclient.NameValuePair;

import com.citi.xylophone.phoneEmulator.audio.AudioManager;
import com.citi.xylophone.phoneEmulator.htmlExecutor.ExecutorInstruction;
import com.citi.xylophone.phoneEmulator.htmlExecutor.model.ExecutorResult;
import com.citi.xylophone.phoneEmulator.model.xml.Param;
import com.citi.xylophone.phoneEmulator.model.xml.vxml.Choice;


public class CoreExecutor {
	
	
	private ExecutorInstruction executorInstruction = null;
	private ExecutorResult executorResult;
	

	public void call(List<Param> params) throws Exception {
		
		//First Request	
		executorInstruction = new ExecutorInstruction();
		executorResult = executorInstruction.executeSimpleRequest(new NameValuePair[0]);
		
		//Second Request		
		String paramsString = "&";
		for (Param param:params){
			paramsString+=param.getType()+"="+param.getValue();
		}
		
		String next = executorResult.getVxml().getForm().getBlock().getSubmit().getNext()+paramsString;	
		executorResult = executorInstruction.executeSimpleRequest(CoreExecutorUtils.getParametersFromURI(next));
		
		AudioManager.playAudio(executorResult.getAudios());
		 
	}
	
	
	
	
	public void inputValue(String value)throws Exception {
		
		if (executorResult.getType().equals("menu")){
			inputValueMenu(value);
			return;
		}
		
		if (executorResult.getType().equals("input")){
			inputValueInput(value);
			return;
		}
		
		if (executorResult.getType().equals("next")){
			next(value);
			return;
		}
		
		
		if (executorResult.getType().equals("transfer")){
			//TODO Disconect
			return;
		}
		
	}
	


	private void next(String value) throws Exception {
		
		String next = executorResult.getVxml().getForm().getBlock().getGotoo().getNext();	
		executorResult = executorInstruction.executeSimpleRequest(CoreExecutorUtils.getParametersFromURI(next));
		AudioManager.playAudio(executorResult.getAudios());
		
	}

	
	
	
	private void inputValueInput(String value) throws Exception {
		
		String next = executorResult.getVxml().getForm().getField().getFilled().getSubmit().getNext();
		String namelist = executorResult.getVxml().getForm().getField().getFilled().getSubmit().getNamelist();
		executorResult = executorInstruction.executeSimpleRequest(CoreExecutorUtils.getParametersFromURI(next+"&"+namelist+"="+value));
		AudioManager.playAudio(executorResult.getAudios());
		
	}

	private void inputValueMenu(String value)throws Exception {
		
		for (Choice choice:executorResult.getVxml().getMenu().getChoiceList()){
			
			if (choice.getDtmf().equals(value)){
				
				executorResult = executorInstruction.executeSimpleRequest(CoreExecutorUtils.getParametersFromURI(choice.getNext()));
				AudioManager.playAudio(executorResult.getAudios());
				return;
			}
		}
		System.out.println("TEST");
		//TODO Temportal FIX to Case no input
		
		executorResult = executorInstruction.executeSimpleRequest(CoreExecutorUtils.getParametersFromURI(executorResult.getVxml().getMenu().getCatchList().get(0).getGotoo().getNext()));
		AudioManager.playAudio(executorResult.getAudios());
		return;
	}

	public void getFinishAudio(String value) throws Exception {
		
		if (executorResult.getType().equals("menu")){
			return;
		}
		
		if (executorResult.getType().equals("input")){
			//TODO time out input
			return;
		}
		
		if (executorResult.getType().equals("next")){
			next(value);
			return;
		}
		
		if (executorResult.getType().equals("transfer")){
			//TODO Disconect
			return;
		}
		
	}
}
